﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using Microsoft.Win32;
using System.Windows.Forms;

namespace SetEnvironment
{
    [RunInstaller(true)]
    public partial class SetEnvironmentInstaller : Installer 
    { 
        public SetEnvironmentInstaller() 
        { 
            InitializeComponent(); 
        } 
        public override void Install(System.Collections.IDictionary stateSaver) 
        { 
            string oraclePath = this.Context.Parameters["targetdir"].ToString() + "OracleClient"; 
            base.Install(stateSaver); SetEnvironment(oraclePath, oraclePath); 
        } 
        private void SetEnvironment(string path, string TNS_ADMIN) 
        { 
            try 
            { 
                using (
                    RegistryKey environment = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\Control\Session Manager\Environment", true))
                { 
                    String pathValue = path + ";" + environment.GetValue("Path").ToString(); environment.SetValue("Path", pathValue); 
                    string search = environment.GetValue("TNS_ADMIN", string.Empty).ToString(); 
                    if (search == string.Empty) 
                    { 
                        environment.SetValue("TNS_ADMIN", TNS_ADMIN); 
                    } 
                    else 
                    { 
                        string value = TNS_ADMIN + ";" + search; environment.SetValue("TNS_ADMIN", value); 
                    } 
                } 
                
                MessageBox.Show("配置环境变量成功"); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
        } 
    }
}
